﻿@using UriBuilder = ResourcesOverMvc.Web.Mvc.Utilities.UriBuilder;
@functions{


    private string _requestedUrlWithoutFormat;
    private bool _helpEnabled;
    private string _menuUri;

    protected string RequestedUrlWithoutFormat
    {
        get { return _requestedUrlWithoutFormat; }
    }

    private bool HelpEnabled
    {
        get
        {
            bool helpEnabled = false;

            string format = this.Request.QueryString["format"];
            if ((format != null) && (format.Equals("help", StringComparison.OrdinalIgnoreCase)))
            {
                helpEnabled = true;
            }

            return helpEnabled;
        }
    }
}
@{
    
 UriBuilder uriBuilder = new UriBuilder(Request.Url);
 uriBuilder.QueryItems.Remove("format");
 _requestedUrlWithoutFormat = uriBuilder.Uri.ToString();

 _helpEnabled = HelpEnabled;
        
if (!_helpEnabled)
{
   uriBuilder.QueryItems["format"] = "help";
}

   _menuUri = uriBuilder.Uri.ToString();
}
<!DOCTYPE html>
<html>
<head>
    <title>@ViewBag.Title</title>
    <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
    <script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
</head>
<body>
    <div class="page">
        <div id="header">
            <div id="title">
                <h1>My MVC Web Service</h1>
            </div>
            <div id="menucontainer">
                <ul id="menu">
                    <li>@Html.ActionLink("Home", "Index", "Home")</li>
                    <li><a href="@_menuUri">@(_helpEnabled ? "Hide Help" : "Show Help")</a></li>
                </ul>
            </div>
        </div>
        <div id="main">
            @RenderBody()

        @if (_helpEnabled)
        {
            <div id="HelpPanel">

                <script type="text/javascript">
                <!--

                    // Enable disable buttons depending on whether there is any message content
                    function enableButtons(length) {
                        if (length > 0) {
                            postButton.disabled = false;
                            putButton.disabled = false;
                        }
                        else {
                            postButton.disabled = true;
                            putButton.disabled = true;
                        }
                    }

                    function callWebService(method) {

                        var url = uriInput.value;
                        var contentType = "*/*";

                        // Append the format variable to the url. For GET we could alternatively set the Accepts
                        // header. Also decide on the Content Type (only of use for POST and PUT) as otherwise
                        // jQuery defaults this to "application/x-www-form-urlencoded". If we were using XmlHttpRequest
                        // directly we could just not use the Content Type header and rely on the format=XYZ
                        // in the URL instead.
                        if (url.indexOf('?', 0) >= 0) {
                            url = url + '&';
                        }
                        else {
                            url = url + '?';
                        }

                        if (xmlRadio.checked) {
                            url = url + "format=xml";
                            contentType = "application/xml";
                        }
                        else if (jsonRadio.checked) {
                            url = url + "format=json";
                            contentType = "application/json";
                        }

                        var dataToSend = "";
                        if (method == "PUT" || method == "POST") {
                            dataToSend = contentsTextBox.value;
                        }


                        jQuery.ajax({
                            type: method,
                            url: url,
                            beforeSend: function (xhr) { xhr.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"); },
                            complete: onRequestComplete,
                            data: dataToSend,
                            contentType: contentType,
                            processData: false
                        });

                        statusInfo.style.display = 'none';
                        timerImage.style.display = 'inline';
                    }

                    function onRequestComplete(xhr) {
                        // Output the results
                        contentsTextBox.value = xhr.responseText;
                        enableButtons(xhr.responseText.length);
                        timerImage.style.display = 'none';
                        statusText.innerHTML = xhr.status + " - " + xhr.statusText;
                        statusInfo.style.display = 'inline';
                        statusTextArea.value = xhr.getAllResponseHeaders()
                    }
                // -->            
                </script>
            
                @RenderSection("HelpText", false)
                
                <form id="testHarness" action="">
                    <h3>Test Harness</h3>
                    <div id="responseInformation">
                        <p id="statusInfo">
                                Status:&nbsp;<span id="statusText"></span><br />
                                <textarea readonly="readonly" id="statusTextArea" cols="30" rows="10" title="Contents of the response headers"></textarea>
                        </p>
                        <p id="timerImage">
                            <img id="RunningImage" src="@Url.Content("~/Content/Running.gif")" class="runningImage" alt="Calling server"/>
                        </p>
                    </div>
                    <div id="commandInformation">
                        <p>
                            Uri:<br />
                            <input id="uriInput" type="text" title="URI to call" value='@RequestedUrlWithoutFormat' /><br />
                        </p>
                        <p>
                            Select the format to be used:<br />
                            <input id="xmlRadio" name="format" type="radio" value="xml" checked="checked" /><label for="xmlRadio">Xml</label><br />
                            <input id="jsonRadio" name="format" type="radio" value="json" /><label for="jsonRadio">Json</label><br />
                            <input id="xhtml" name="format" type="radio" value="xhtml" /><label for="jsonRadio">Xhtml</label><br />
                        </p>
                    </div>
                    
                        <input id="getButton" type="button" title="Send a GET request" value="GET" onclick="callWebService('GET');return false;"/>
                        <input id="postButton" type="button" title="Send a POST request" value="POST" disabled="disabled" onclick="callWebService('POST');return false;"/>
                        <input id="putButton" type="button" title="Send a PUT request" value="PUT" disabled="disabled" onclick="callWebService('PUT');return false;"/>
                        <input id="deleteButton" type="button" title="Send a DELETE request" value="DELETE" onclick="callWebService('DELETE');return false;"/>
                    
                    <textarea id="contentsTextBox" cols="20" rows="30" title="Contents of the message"  onkeyup="enableButtons(this.value.length)" onchange="enableButtons(this.value.length)"></textarea>
                </form>
                
                <script type="text/javascript">
                <!--

                    var baseUrlWithoutFormatVariable = '<%= RequestedUrlWithoutFormat %>';
                    var uriInput = document.getElementById("uriInput");
                    var xmlRadio = document.getElementById("xmlRadio");
                    var jsonRadio = document.getElementById("jsonRadio");
                    var postButton = document.getElementById("postButton");
                    var putButton = document.getElementById("putButton");
                    var contentsTextBox = document.getElementById("contentsTextBox");
                    var statusInfo = document.getElementById("statusInfo");
                    var statusText = document.getElementById("statusText");
                    var statusTextArea = document.getElementById("statusTextArea");
                    var timerImage = document.getElementById("timerImage");
                    var runningImage = document.getElementById('<%= RunningImage.ClientID %>');
                    
                //-->
                </script>          
            </div>
        }
        else
        {
            // This is a hack to get around a limitation with the razor world whereby if a view tries
            // to set the content of a non-existent section an exeception is thrown. This code just swallows
            // the rendered text. Interestingly, the ASPX aproach does not have this issue.
            WriteTo(new StringWriter(), RenderSection("HelpText", false));
        }         
        </div>
        <div id="footer">
        </div>
    </div>
</body>
</html>
